/*
 * @Author: CodeHare
 * @Date: 2023-11-30 16:45:54
 * @LastEditors: CodeHare
 * @LastEditTime: 2023-12-25 10:48:24
 * @Description: 
 */
// ignore_for_file: unnecessary_overrides

import 'dart:io';

import 'package:get/get.dart';
import 'package:native_dart_package/store/sp_store.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/utils/constants/constant_string.dart';
import 'package:yt_devops/pages/mine/about_us/about_us_model.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/device/device_sevices.dart';
import 'package:yt_devops/utils/constants/store_keys.dart';
import 'package:yt_devops/utils/update_dialog.dart';

class AboutUsController extends GetxController {
  final AboutUsState state = AboutUsState();
  final AboutInfoState aboutInfoState = AboutInfoState();
  RefreshController refreshcontroller = RefreshController();
  DeviceServices services = Get.put(DeviceServices());
  bool isNeedToUpdate = false;//是否需要去更新
  String updateString = ResKey.isLatestVersion.tr;
  var count = 0.obs;
  var  clickCount = 0;
  @override
  void onInit() {
    super.onInit();
    getAppInfo();
    Future.delayed(Duration(seconds: 1), () {
      getAppInfo();
    });
    _getAppVersion();
  }

  /// 获取APP信息
  void getAppInfo() {
    PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
      aboutInfoState.appName = packageInfo.appName;
      aboutInfoState.packageName = packageInfo.packageName;
      aboutInfoState.version = packageInfo.version;
      aboutInfoState.buildNumber = packageInfo.buildNumber;
      update();
    });
  }
  void increment() {
    count ++; // 使用update方法更新Observable
  }
  ///获取app版本
  _getAppVersion() async {
    // platform是平台 0安卓 苹果1  appType是app类型 1代表E智慧能源app
    var platform = '1';
    if (Platform.isAndroid) {
      platform = '0';
    }
    var result =
    await services.getAppVersion({'platform': platform, 'appType': '2'});
    if (result != null &&
        result['current'] != null &&
        result['current']['version'] != null) {
      String version = result['current']['version'].toString();
      int versionCode = int.parse(version.replaceAll('.', ''));
      _checkUpdate(versionCode, version);
    }
  }

  //检查是否需要升级
  _checkUpdate(int serviceVersionCode, String lastVersion) async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    //获取当前的版本号
    int currentVersionCode = int.parse(packageInfo.version.replaceAll('.', ''));

    //如果获取服务器的版本号比当前应用程序的版本号还高，且保存的日期不等于当前日期 那么提示升级(当天只提示一次), 否则检查是否有新的补丁包
    if (serviceVersionCode > currentVersionCode) {
      isNeedToUpdate = true;
      updateString = ResKey.gotoUpdate.tr;
    } else {
      isNeedToUpdate = false;
      updateString = ResKey.isLatestVersion.tr;
    }


  }
  ///去更新
  Future<void> goToUpdate() async {
    if(isNeedToUpdate){
      ///iOS直接打开App Store
      if (Platform.isIOS) {
        launchUrlString(
          "itms-apps://itunes.apple.com/app/${ConstantString.appId}",
        );
        ///Android打开浏览器下载
      } else if (Platform.isAndroid) {
        await launchUrlString(ConstantString.androidDownloadUrl, mode: LaunchMode.externalApplication);
      }
    }

  }
  ///去评分页面
  gotoMark(){
    if (Platform.isIOS) {
      launchUrlString(
        'itms-apps://itunes.apple.com/app/id${ConstantString.appId}?action=write-review',
      );
    }
  }
}
